package cn.iocoder.yudao.module.design.controller.admin.reform;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.reform.vo.*;
import cn.iocoder.yudao.module.design.convert.reform.ReformConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.reform.ReformDO;
import cn.iocoder.yudao.module.design.service.reform.ReformService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 项目整改单")
@RestController
@RequestMapping("/design/reform")
@Validated
public class ReformController {

    @Resource
    private ReformService reformService;

    @PostMapping("/create")
    @Operation(summary = "创建项目整改单")
    @PreAuthorize("@ss.hasPermission('design:reform:create')")
    public CommonResult<Integer> createReform(@Valid @RequestBody ReformCreateReqVO createReqVO) {
        return success(reformService.createReform(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目整改单")
    @PreAuthorize("@ss.hasPermission('design:reform:update')")
    public CommonResult<Boolean> updateReform(@Valid @RequestBody ReformUpdateReqVO updateReqVO) {
        reformService.updateReform(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目整改单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:reform:delete')")
    public CommonResult<Boolean> deleteReform(@RequestParam("id") Integer id) {
        reformService.deleteReform(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目整改单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:reform:query')")
    public CommonResult<ReformRespVO> getReform(@RequestParam("id") Integer id) {
        ReformDO reform = reformService.getReform(id);
        return success(ReformConvert.INSTANCE.convert(reform));
    }

    @GetMapping("/list")
    @Operation(summary = "获得项目整改单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:reform:query')")
    public CommonResult<List<ReformRespVO>> getReformList(@RequestParam("ids") Collection<Integer> ids) {
        List<ReformDO> list = reformService.getReformList(ids);
        return success(ReformConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目整改单分页")
    @PreAuthorize("@ss.hasPermission('design:reform:query')")
    public CommonResult<PageResult<ReformRespVO>> getReformPage(@Valid ReformPageReqVO pageVO) {
        PageResult<ReformDO> pageResult = reformService.getReformPage(pageVO);
        return success(ReformConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目整改单 Excel")
    @PreAuthorize("@ss.hasPermission('design:reform:export')")
   
    public void exportReformExcel(@Valid ReformExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ReformDO> list = reformService.getReformList(exportReqVO);
        // 导出 Excel
        List<ReformExcelVO> datas = ReformConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目整改单.xls", "数据", ReformExcelVO.class, datas);
    }

}
